<?php
/*
+--------------------------------------------------------------------------
|   WeCenter [#RELEASE_VERSION#]
|   ========================================
|   by WeCenter Software
|   © 2011 - 2014 WeCenter. All Rights Reserved
|   http://www.wecenter.com
|   ========================================
|   Support: WeCenter@qq.com
|
+---------------------------------------------------------------------------
*/

define('IN_AJAX', TRUE);


if (!defined('IN_ANWSION'))
{
	die;
}

class ajax extends AWS_CONTROLLER
{
	public function get_access_rule()
	{
		$rule_action['rule_type'] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查

		$rule_action['actions'] = array(
			'login',
            'reg',
            'sendCode',
            'getUserInfoById'

		);

		return $rule_action;
	}



	public function setup()
	{
		HTTP::no_cache_header();
	}

	public function login_action(){

        $result = file_get_contents('php://input');
        $data = json_decode($result, true);
        $phone = $data['phone'];
        $password = $data['password'];
        $user_name = $phone."_im";
        $user_info = $this->model('account')->check_login($user_name, $password);
        $req_data  = array();
        if($user_info){
            $expire = 60 * 60 * 24 * 360;
            $this->model('account')->setcookie_login($user_info['uid'], $_POST['user_name'], $_POST['password'], $user_info['salt'], $expire);
            $req_data['code'] = 200;
            $result_data = array();
            $result_data['id'] = $user_name;
            $result_data['token'] = $user_info['token'];
            $req_data['result'] = $result_data;

            //H::ajax_json_output(AWS_APP::RSM(jsonp_encode($user_info), 1, AWS_APP::lang()->_t('发送成功')));
        }else{
            $req_data['code'] = 100;

        }
        echo  json_encode($req_data);



    }
    public function reg_action(){

        $result = file_get_contents('php://input');
        $data = json_decode($result, true);
        $phone = $data['phone'];
        $pwd = $data['password'];
        $name = $data['nickname'];
        $user_name = $phone."_im";
        $code = $data['code'];

        if($code != '8888'){
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('验证码错误')));
        }
        else if ($this->model('account')->check_mobile($phone))
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('手机号已被注册')));
        }
        $uid = $this->model('account')->new_user_register($user_name,$pwd,$phone,1,$name);
        $res = $this->model('api')->reg_user($user_name,$name);
        $user_info = $this->model('account')->get_user_info_by_uid($uid);
        $this->model('account')->setcookie_login($user_info['uid'], $user_info['user_name'], $pwd, $user_info['salt'],$user_name);

        $obj=json_decode($res);
        $req_data = array();

        if($obj->code == '200'){
            $token = $obj->token;
            $update_data = array();
            $update_data['token'] = $token;
            $this->model('account')->update_users_fields($update_data, $uid);

            $req_data['code'] = 200;
            $req_data['token'] = $token;
            $req_data['id'] = $user_name;

            echo  json_encode($req_data);
            // H::ajax_json_output(AWS_APP::RSM(null, 1, AWS_APP::lang()->_t('注册成功')));
        }else{
            $this->model('account')->delete('users', 'uid = ' . intval($uid));
            $req_data['code'] = 100;
            $req_data['token'] = "";
            echo  json_encode($req_data);
            //H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('注册失败')));

        }

    }

    public function getUserInfoById_action(){

        //  * result : {"id":"10YVscJI3","nickname":"阿明","portraitUri":""}
        $id = $_GET['id'];
        $user_info = $this->model('account')->get_user_info_by_name($id);
        $req_data = array();
        if($user_info){
            $req_data['code'] = 200;
            $res_data = array();
            $res_data['id'] = $user_info['user_name'];
            $res_data['nickname'] = $user_info['real_name'];
            $res_data['portraitUri'] = "";
            $req_data['result'] = $res_data;

        }else{
            $req_data['code'] = 100;

        }
        echo json_encode($req_data);

    }

    public function sendCode_action(){

        H::ajax_json_output(AWS_APP::RSM(null, 1, AWS_APP::lang()->_t('发送成功')));

    }



}
